package com.mini.framework.record;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DefaultFormatter implements RecordFormatter {
	
	private DateFormat timeFormat;
	
	public DefaultFormatter(){
		timeFormat=new SimpleDateFormat(RecordConfig.DATETIME_FORMAT);
	}

	

	public String format(Object[] fields) {
		if(fields==null||fields.length==0){
			return null;
		}

		StringBuilder sb=new StringBuilder();

		sb.append(getFieldString(fields[0]));

		for(int i=1;i<fields.length;i++){

			sb.append(RecordConfig.FILED_DELIMETER);

			sb.append(getFieldString(fields[i]));

		}
		return sb.toString();
		
	}
	
	private String getFieldString(Object field){
		if(field==null){
			return "";
		}
		if(field instanceof Date){
			synchronized(timeFormat){
				return timeFormat.format((Date)field);
			}
		}
		return field.toString();

	}

}
